﻿using System;
using Csla.Xaml;
using Accounting.Library;
using Accounting.Library.Utility;
using Utility;


namespace csla4Skeleton.UI.SL.TongHop.TH_NghiepVuKhac
{
    public class TH_NghiepVuKhac_ViewModel : ViewModel<Accounting.Library.TH_NghiepVuKhac>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
        public delegate void TKNoIdStartWithSix_EventHandler(object sender, TKNoIdStartWithSixEventArgs e);
        public delegate void DoiTuongTHCPIDChanged_EventHandler(object sender, DoiTuongTHCPIDChangedEventArgs e);

        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;
        public event TKNoIdStartWithSix_EventHandler TKNoIdStartWithSix;
        public event DoiTuongTHCPIDChanged_EventHandler DoiTuongTHCPIDChanged;
        #endregion

        #region Refresh Model

        public void SaveAndNew()
        {
            BeginSave();
            NewTH_NghiepVuKhac_ViewModel();
        }

        public void NewTH_NghiepVuKhac_ViewModel()
        {
            BeginRefresh("NewTH_NghiepVuKhac");
        }

        public TH_NghiepVuKhac_ViewModel()
        {
            BeginRefresh("NewTH_NghiepVuKhac");
        }

        public TH_NghiepVuKhac_ViewModel(int id)
        {
            BeginRefresh("GetTH_NghiepVuKhac", id);
        }

        public void GetTH_NghiepVuKhac_ViewModell(int id)
        {
            BeginRefresh("GetTH_NghiepVuKhac", id);
        }

        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.TH_NghiepVuKhac_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<TH_NghiepVuKhac_ChiTietChild>>(TH_NghiepVuKhac_ChiTiets_AddedNew);
                Model.TKNoIdStartWithSix += new EventHandler<TKNoIdStartWithSixEventArgs>(OnTKNoIdStartWithSix);
                Model.DoiTuongTHCPIDChanged += new EventHandler<DoiTuongTHCPIDChangedEventArgs>(OnDoiTuongTHCPIDChanged);
            }
        }
        #endregion

        #region Add Order Details Item
        public void AddOrderDetailsItem()
        {
            Model.TH_NghiepVuKhac_ChiTiets.AddNewItem(Model.Khoa);
            Model.TH_NghiepVuKhac_Thues.AddNewItem(Model.Khoa);
           
        }
        void TH_NghiepVuKhac_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<TH_NghiepVuKhac_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("TH_NghiepVuKhac_ChiTiets"));
        }

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }
        public void OnTKNoIdStartWithSix(object sender, TKNoIdStartWithSixEventArgs e)
        {
            if (TKNoIdStartWithSix != null)
            {
                TKNoIdStartWithSix(sender, e);
            }

        }
        #endregion

        #region Nghiep Vu Methods
        internal void DeleteTH_NghiepVuKhac()
        {
            int orderID = this.Model.Khoa;
            this.IsBusy = true;
            Accounting.Library.TH_NghiepVuKhac.DeleteTH_NghiepVuKhac(orderID, (o, ex) =>
            {
                BeginRefresh("NewTH_NghiepVuKhac");
            });
            this.IsBusy = false;
        }
        public void OnDoiTuongTHCPIDChanged(object sender, DoiTuongTHCPIDChangedEventArgs e)
        {
            if (DoiTuongTHCPIDChanged != null)
            {
                DoiTuongTHCPIDChanged(sender, e);
            }

        }
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_ChungTuNghiepVuKhac;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetTH_NghiepVuKhac", Model.Khoa);
            });
            this.IsBusy = false;
        }
        #endregion
    }
}
